const dao = require("../Dao/Dao.js")

module.exports = {
    register(req, resp) {
        let param = []
        for (let i in req.query) {
            param.push(req.query[i])
        }
        let sql = `INSERT INTO users (\`name\`,\`status\`,\`class\`,\`password\`,\`phone\`,\`qq\`,\`department\`) VALUES (?,?,?,?,?,?,?); `
        dao.do(sql, param, req.query['timer'], function (err, data) {
            resp.send(data)
            err ? console.log(err) : null
        })
    },
    async login(req, resp) {
        let [phone, password] = [req.query.phone || '', req.query.password || '']
        resp.send(await database.user.findAll({ where: { phone, password } }))
    },
    async getUserInfo(req, resp) {
        let uinfo = await database.user.findOne({
            include: [{ model: database.identity }],
            where: { id: req.query.uId }
        })
        resp.send(uinfo)
    },
    async getDepartmentPerson(req, resp) {
        let users
        if (req.query['department'] == 0) {
            users = await database.user.findAll();
        } else {
            // let dept_id = await database.department.findByPk(req.query['department']).did || -1
            users = await database.user.findAll({ where: { dept_id: req.query['department'] } });
        }
        resp.send(users)
    }
}